﻿@using SmartCode
@using SmartCode.Db
@model BuildContext
@{
    var project = Model.Project;
    var dbSource = Model.GetDataSource<DbSource>();
    var dbPrefix = dbSource.DbProvider.ParameterPrefix;
    var buildTask = Model.Build;
    var table = Model.GetCurrentTable();
    var pkCol = table.PKColumn;
    if (pkCol == null)
    {
        throw new SmartCodeException($"{table.Name} can not find PKColumn！");
    }
}

<SmartSqlMap Scope="@table.ConvertedName" xmlns="http://SmartSql.net/schemas/SmartSqlMap.xsd">
    @{ await Html.RenderPartialAsync("SqlMap-ResultMaps.cshtml", Model); }
    <Statements>
        @{ await Html.RenderPartialAsync("SqlMap-Columns.cshtml", Model); }
        <Statement Id="QueryParams">
            <Where>
                @foreach (var col in table.Columns)
                {
                    <IsNotEmpty Prepend="And" Property="@col.ConvertedName">
                        T.@col.Name = @dbPrefix@col.ConvertedName
                    </IsNotEmpty>
                }
            </Where>
        </Statement>

        @if (IsSectionDefined("Insert"))
        {
            await RenderSectionAsync("Insert", required: false);
        }
        else
        {
            await Html.RenderPartialAsync("./CURD/SqlMap-Insert.cshtml", Model);
        }

        @if (IsSectionDefined("Delete"))
        {
            await RenderSectionAsync("Delete", required: false);
        }
        else
        {
            await Html.RenderPartialAsync("./CURD/SqlMap-Delete.cshtml", Model);
        }

        @if (IsSectionDefined("Update"))
        {
            await RenderSectionAsync("Update", required: false);
        }
        else
        {
            await Html.RenderPartialAsync("./CURD/SqlMap-Update.cshtml", Model);
        }

        @if (IsSectionDefined("Query"))
        {
            await RenderSectionAsync("Query", required: false);
        }
        else
        {
            await Html.RenderPartialAsync("./CURD/SqlMap-Query.cshtml", Model);
        }

        @if (IsSectionDefined("QueryByPage"))
        {
            await RenderSectionAsync("QueryByPage", required: false);
        }
        else
        {
            await Html.RenderPartialAsync("./CURD/SqlMap-QueryByPage.cshtml", Model);
        }

        @if (IsSectionDefined("GetRecord"))
        {
            await RenderSectionAsync("GetRecord", required: false);
        }
        else
        {
            await Html.RenderPartialAsync("./CURD/SqlMap-GetRecord.cshtml", Model);
        }

        @if (IsSectionDefined("GetEntity"))
        {
            await RenderSectionAsync("GetEntity", required: false);
        }
        else
        {
            await Html.RenderPartialAsync("./CURD/SqlMap-GetEntity.cshtml", Model);
        }

        @if (IsSectionDefined("IsExist"))
        {
            await RenderSectionAsync("IsExist", required: false);
        }
        else
        {
            await Html.RenderPartialAsync("./CURD/SqlMap-IsExist.cshtml", Model);
        }

    </Statements>
</SmartSqlMap>